/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

/*


 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.clean;

import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import storage.dbhelper.NoiseIdentifyRuleHelper;
import storage.dbobject.DbNoiseIdentifyRule;
import cn.edu.thu.log.web.service.PropertiesReaderServices;
//import globalparameter.TempFilePosition;

/**
 * 
 * @author wan
 */
public class NoiseConfigSave {
	private String filename;
/**
 * 
 * @param newFilename parameter
 * @param treexml parameter
 */
	public void writeNoiseConfig(String newFilename, String treexml) {
		// System.out.println("In the CleanConfigSave!");
		// System.out.println("Begin to write to noise file!");
		OutputStreamWriter osw = null;
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		try {
			String filepath = x1.getProperties("filepath") + newFilename + ".xml";
			osw = new OutputStreamWriter(new FileOutputStream(filepath));
			String xmlhead = x1.getProperties("xml_head");
			String wholexml = xmlhead + treexml;
			osw.write(wholexml);
			osw.flush();

			// System.out.println("Begin to write to cassandra!");
			NoiseIdentifyRuleHelper noiseH = new NoiseIdentifyRuleHelper();
			Date currentTime = new Date();
			DbNoiseIdentifyRule dbnoise = new DbNoiseIdentifyRule(newFilename,
					wholexml.toString(), currentTime.toString());
			noiseH.saveNoiseRule(dbnoise);
			osw.close();


		} catch (FileNotFoundException ex) {
			Logger.getLogger(NoiseConfigSave.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (IOException ex) {
			Logger.getLogger(NoiseConfigSave.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	private boolean deleteFile(String sPath) {
		File file = new File(sPath);
		// 路径为文件且不为空则进行删除
		if (file.isFile() && file.exists()) {
			file.delete();
			return true;
		}
		return false;
	}

	/**
	 * @return the filename
	 */
	public String getFilename() {
		return filename;
	}

	/**
	 * @param filename
	 *            the filename to set
	 */
	public void setFilename(String filename) {
		this.filename = filename;
	}

}
